﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 短句管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PhraseManagementController : AdminBaseController
    {
        /// <summary>
        /// 短句管理首页
        /// </summary>
        /// <returns>首页页面</returns>
        [AuthorityActionFilter("m_SysManager", Order = 1)]
        [LogPageActionFilter("短句管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 短句管理列表页
        /// </summary>
        /// <returns>列表信息业</returns>
        public ActionResult PhraseManagementList()
        {

            int pageIndex = 1;
            List<Model.SysPhrase> phraseManagementList = GetPhraseManagementList("", pageIndex);

            return View(phraseManagementList);

        }





        /// <summary>
        /// 获得短语数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的诗句</returns>
        private List<Model.SysPhrase> GetPhraseManagementList(string strWhere, int pageIndex)
        {
            BLL.SysPhrase commonBll = new BLL.SysPhrase();


            string fiedls = "*";
            int total;
            List<ElingOA6_Admin_DSJCJ.Model.SysPhrase> phraseManagementList = commonBll.GetListByPage("v_SysPhrase", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "FIsTop", 1, out total);

            //BLL.SysPhrase phraseManagementBll = new BLL.SysPhrase();
            List<Model.SysPhrase> phraseManagementAllList = new List<Model.SysPhrase>();
            //List<Model.SysPhrase> phraseManagementList = phraseManagementBll.DataTableToList1(ds.Tables[0]);

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                phraseManagementAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (phraseManagementList.Count > i)
                {
                    phraseManagementAllList[((pageIndex - 1) * pageRowSize) + i] = phraseManagementList[i];
                }
            }

            return phraseManagementAllList;


        }




        /// <summary>
        /// 短语管理grid
        /// </summary>
        /// <returns> 短语管理grid</returns>
        public ActionResult PhraseManagementListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string deptNo = Request.Params["deptNo"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            string type = Request.Params["Type"];
            string gNO = Request.Params["gNO"];
            string dutyNo = Request.Params["DutyNo"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }

            List<Model.SysPhrase> phraseManagementList = GetPhraseManagementList(command,pageIndex);


            return PartialView("PhraseManagementListPartial", phraseManagementList);
        }
        /// <summary>
        /// 删除短句信息（单个）
        /// </summary>
        /// <param name="phraseNo">短句编号</param>
        /// <returns>是否成功</returns>
        public string DelPhraseManagemen(string phraseNo)
        {
            try
            {
                BLL.SysPhrase sysPhraseBLL = new BLL.SysPhrase();
                sysPhraseBLL.Delete(Common.GlobalConstant.ENTITYNO, decimal.Parse(phraseNo));
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        /// <summary>
        /// 删除短句信息（多个）
        /// </summary>
        /// <param name="phraseNoArray">短句编号逗号分隔</param>
        /// <returns>是否成功</returns>
        public string DeletePhraseManagemen(string phraseNoArray)
        {
            try
            {
                BLL.SysPhrase sysPhraseBLL = new BLL.SysPhrase();
                sysPhraseBLL.DeleteList(phraseNoArray);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

       /// <summary>
        /// 数据增加修改保存
        /// </summary>
        ///  <param name="phraseManagement">修改数据</param>
       ///  <returns>保存修改后页面</returns>
        public ActionResult Save(Model.SysPhrase phraseManagement)
        {
            try
            {
                BLL.SysPhrase sysPhraseBLL = new BLL.SysPhrase();
                switch (Fetch.Post("hdMethod"))
                {
                    case "ADD":
                        phraseManagement.FPubDate = DateTime.Now;
                        // PhraseManagement.FPhraseNo = Fetch.Post("FPhraseNo");
                        if (Fetch.Post("FIsTop") == "C")
                        {
                            phraseManagement.FIsTop = 1;
                        }
                        else
                        {
                            phraseManagement.FIsTop = 0;
                        }
                        phraseManagement.FEntityNo = Common.GlobalConstant.ENTITYNO;
                        phraseManagement.FDeptNo = OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO);
                        phraseManagement.FPubIp = Common.GenerateUtil.GetClientIPv4Address();
                        phraseManagement.FEmplNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                        sysPhraseBLL.Add(phraseManagement);
                        break;
                    case "Edit":
                        phraseManagement.FPhraseID = int.Parse(Fetch.Post("hdphraseNo"));
                        Model.SysPhrase sysPhraseModel = new Model.SysPhrase();
                        sysPhraseModel = sysPhraseBLL.GetModel(int.Parse(Fetch.Post("hdphraseNo")));
                        sysPhraseModel.FPhraseNo = phraseManagement.FPhraseNo;
                        if (Fetch.Post("FIsTop") == "C")
                        {
                            sysPhraseModel.FIsTop = 1;
                        }
                        else
                        {
                            sysPhraseModel.FIsTop = 0;
                        }
                        sysPhraseModel.FPhrase = phraseManagement.FPhrase;
                        sysPhraseModel.FURL = phraseManagement.FURL;
                        phraseManagement.FDeptNo = sysPhraseModel.FDeptNo;
                        sysPhraseBLL.Update(sysPhraseModel);
                        break;
                }
                ViewData["jsAlert"] = "OK";
            }
            catch
            {
                ViewData["jsAlert"] = "error";
            }

            BLL.HRDept hRDeptBLL = new BLL.HRDept();
            BLL.SysBasicCodeName sysBasicCodeNameBLL = new BLL.SysBasicCodeName();
            ViewData["DeptName"] = hRDeptBLL.GetDeptName(Common.GlobalConstant.ENTITYNO, phraseManagement.FDeptNo);
            ViewData["SYS_PHRSList"] = sysBasicCodeNameBLL.GetListByClsNo("SYS_PHRS", Common.GlobalConstant.ENTITYNO);
            ViewData["hdMethod"] = Fetch.Post("hdMethod");
            ViewData["phraseNo"] = phraseManagement.FPhraseID;
            BLL.HREmpl hREmplBLL = new BLL.HREmpl();
            phraseManagement.FEmpName = hREmplBLL.GetEmpName(Common.GlobalConstant.ENTITYNO, phraseManagement.FEmplNo);
            return View("PhraseManagementEdit", phraseManagement);
        }
        /// <summary>
        /// 短句增加修改页面初始化
        /// </summary>
        /// <param name="phraseNo">短句编号</param>
        /// <returns>初始化页面</returns>
        public ActionResult PhraseManagementEdit(string phraseNo)
        {
            Model.SysPhrase sysPahraseModel = new Model.SysPhrase();
            if (phraseNo == null)
            {
                sysPahraseModel.FEmpName = OperateSession.get(Common.GlobalConstant.PARAM_USERNAME);
                sysPahraseModel.FPubDate = DateTime.Now;
                BLL.HRDept hRDeptBLL = new BLL.HRDept();
                BLL.SysBasicCodeName sysBasicCodeNameBLL = new BLL.SysBasicCodeName();
                ViewData["DeptName"] = hRDeptBLL.GetDeptName(Common.GlobalConstant.ENTITYNO, OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO));
                ViewData["SYS_PHRSList"] = sysBasicCodeNameBLL.GetListByClsNo("SYS_PHRS", Common.GlobalConstant.ENTITYNO);
                ViewData["hdMethod"] = "ADD";
                ViewData["jsAlert"] = "";
                ViewData["phraseNo"] = "";
            }
            else
            {
                BLL.SysPhrase sysPhraseBLL = new BLL.SysPhrase();
                BLL.HRDept hRDeptBLL = new BLL.HRDept();
                BLL.SysBasicCodeName sysBasicCodeNameBLL = new BLL.SysBasicCodeName();
                sysPahraseModel = sysPhraseBLL.GetModel(decimal.Parse(phraseNo));
                ViewData["DeptName"] = hRDeptBLL.GetDeptName(Common.GlobalConstant.ENTITYNO, sysPahraseModel.FDeptNo);
                ViewData["SYS_PHRSList"] = sysBasicCodeNameBLL.GetListByClsNo("SYS_PHRS", Common.GlobalConstant.ENTITYNO);
                ViewData["hdMethod"] = "Edit";
                ViewData["jsAlert"] = "";
                ViewData["phraseNo"] = phraseNo;
                BLL.HREmpl hREmplBLL=new BLL.HREmpl();
                sysPahraseModel.FEmpName = hREmplBLL.GetEmpName(Common.GlobalConstant.ENTITYNO,sysPahraseModel.FEmplNo);
            }
            
            return View(sysPahraseModel);
        }



    }

    

}
